/**
 * 选项分类设置 - 编辑页面
 * @author miaoyl
 * @date 2019/03/05
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        vueData = {
            isAdd: false,
            isCheck: false,
            form: {
                GWDE001_old: ''
            },
            disabled:false,
            rules: {
                GWDE001: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWDE002: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ]
            },
            selectOptions: {
                sys_y_n: app.datas_("sys_y_n"),
                crm_gwdc006_gwde017:[]
            }
        },
        cols = [[
            {field: 'INDEX_',type: 'checkbox', title: '选择'}
            , {field: 'GWDE003', width: 140, title: '选项值', edit:'text'}
            , {field: 'GWDE004', width: 140, title: '选项说明', edit:'text'}
            , {field: 'GWDE011', width: 140, title: '净发货额',
                filterDatas: [{code:'',value:'请选择'}].concat(app.datas_('crm_gwdc006_gwde011')),
                edit:'select',hide:true}
            , {field: 'GWDE012', width: 140, title: '无返利发货调整金额',
                filterDatas: [{code:'',value:'请选择'}].concat(app.datas_('crm_gwdc006_gwde011')),
                edit:'select',hide:true}
            , {field: 'GWDE013', width: 160, title: '不计入指标发货调整金额',
                filterDatas: [{code:'',value:'请选择'}].concat(app.datas_('crm_gwdc006_gwde011')),
                edit:'select',hide:true}
            , {field: 'GWDE014', width: 140, title: '铺货按回款调整金额',
                filterDatas: [{code:'',value:'请选择'}].concat(app.datas_('crm_gwdc006_gwde011')),
                edit:'select',hide:true}
            , {field: 'GWDE015', width: 140, title: '逾期款调整金额',
                filterDatas: [{code:'',value:'请选择'}].concat(app.datas_('crm_gwdc006_gwde011')),
                edit:'select',hide:true}
            , {field: 'GWDE016', width: 140, title: '系统外调整金额',
                filterDatas: [{code:'',value:'请选择'}].concat(app.datas_('crm_gwdc006_gwde011')),
                edit:'select',hide:true}
            , {field: 'GWDE017', width: 140, title: '影响返利类型',
                filterDatas: [{code:'',value:'请选择'}].concat(vueData.selectOptions.crm_gwdc006_gwde017),
                edit:'select',hide:true}
            /*, {field: 'GWDE006', width: 140, title: '裸价记录指标否',
                filterDatas: [{code:'',value:'请选择'}].concat(app.datas_('crm_gwai501_gwax011')),
                edit:'select'}
            , {field: 'GWDE007', width: 140, title: '裸价参加返利否',
                filterDatas: [{code:'',value:'请选择'}].concat(app.datas_('crm_gwai501_gwax011')),
                edit:'select'}*/
        ]],
        callObj,
        listData = null,
        table,
        gwdc006Table,
        pageVue
    ;

    var Gwdc006Edit = app.Module.extend({
            init: function () {
                self = this;
                self._super({
                    _tag: 'gwdc006_edit'
                }, win);
            },

            initTable: function () {
                layui.use(['table'], function () {
                    table = layui.table;
                    gwdc006Table = table.render({
                        elem: app.getByid(doc, 'gwdc006_edit_table'),
                        id: 'gwdc006_edit_table',
                        cols: cols,
                        height:250,
                        page: false
                    });

                });
            },

            tableReload: function () {
                /*gwdc006Table.reload({
                    data: listData
                });*/
                self._reloadTable({id:'gwdc006_edit_table',cols:cols,data: listData});
            },

            addAllData: function (listData1) {
                app.ajax({
                    xurl: 'crm/gwdc006/add',
                    xdata: listData1,
                    loading: true,
                    toJsonStr: true,
                    okmsg: self._moduleName + '增加成功',
                    cb: function (result) {
                        if (result.ok) {
                            app.pageModule('gwdc006').reloadTableCurr();
                            self._closeTpl();
                        }
                    }
                });
            },

            editAllData: function (listData1) {
                app.ajax({
                    xurl: 'crm/gwdc006/edit',
                    xdata: {list1:listData1, GWDE001_old: vueData.form.GWDE001_old},
                    loading: true,
                    toJsonStr: true,
                    okmsg: self._moduleName + '修改成功',
                    cb: function (result) {
                        if (result.ok) {
                            app.pageModule('gwdc006').reloadTableCurr();
                            self._closeTpl();
                        }
                    }
                });
            },

            _call: function (tagData) {
                pageVue.$refs['form'].resetFields();
                if (tagData.isCheck) {
                    vueData.isCheck = true;
                } else {
                    vueData.isCheck = false;
                }
                if (tagData.is_add) {
                    vueData.isAdd = true;
                    vueData.disabled = false;
                    app.extendUpLow({
                        target: vueData.form,
                        res: {
                            GWDEACTI: 'Y'
                        },
                        reset: true
                    });
                    listData = [];
                    //self.tableReload();
                } else {
                    vueData.isAdd = false;
                    vueData.disabled = true;
                    callObj = tagData.obj;
                    app.extendUpLow({
                        target: vueData.form,
                        res: callObj.data,
                        reset: true
                    });
                    vueData.form.GWDE001_old = callObj.data.GWDE001;
                    app.ajax({
                        async:false,
                        xurl: 'crm/gwdc006/getGwdeData',
                        xdata: {GWDE001: callObj.data.GWDE001},
                        cb: function (result) {
                            if(result.ok) {
                                listData = result.data.data;
                            }
                            //self.tableReload();
                        }
                    });
                }
                app.ajax({
                    async:false,
                    xurl: 'crm/gwdc006/getGwde017List',
                    xdata: {GWDE002:"返利编号"},
                    cb: function (result) {
                        if(result.ok) {
                            vueData.selectOptions.crm_gwdc006_gwde017 = result.data.data;
                            gwdc006Table.config.cols[0][9].filterDatas = [{code:'',value:'请选择'}].concat(vueData.selectOptions.crm_gwdc006_gwde017);
                        }
                    }
                });
                if(vueData.form.GWDE002 == '返利编号'){
                    gwdc006Table.config.cols[0][3].hide=false;
                    gwdc006Table.config.cols[0][4].hide=false;
                    gwdc006Table.config.cols[0][5].hide=false;
                    gwdc006Table.config.cols[0][6].hide=false;
                    gwdc006Table.config.cols[0][7].hide=false;
                    gwdc006Table.config.cols[0][8].hide=false;
                }else {
                    gwdc006Table.config.cols[0][3].hide=true;
                    gwdc006Table.config.cols[0][4].hide=true;
                    gwdc006Table.config.cols[0][5].hide=true;
                    gwdc006Table.config.cols[0][6].hide=true;
                    gwdc006Table.config.cols[0][7].hide=true;
                    gwdc006Table.config.cols[0][8].hide=true;
                }
                if(vueData.form.GWDE002 == '外部返利类型'){
                    gwdc006Table.config.cols[0][9].hide=false;
                }else {
                    gwdc006Table.config.cols[0][9].hide=true;
                }
                self.tableReload();
            },

            _ready: function () {
                table = layui.table;
                vueData.form = app.fields_(['gwde_t'],vueData.form);
                pageVue = new Vue({
                    el: '#gwdc006_edit',
                    data: vueData,
                    watch: {},
                    methods: {
                        gwde002Change:function () {
                            if(vueData.form.GWDE002 == '返利编号'){
                                gwdc006Table.config.cols[0][3].hide=false;
                                gwdc006Table.config.cols[0][4].hide=false;
                                gwdc006Table.config.cols[0][5].hide=false;
                                gwdc006Table.config.cols[0][6].hide=false;
                                gwdc006Table.config.cols[0][7].hide=false;
                                gwdc006Table.config.cols[0][8].hide=false;
                            }else {
                                gwdc006Table.config.cols[0][3].hide=true;
                                gwdc006Table.config.cols[0][4].hide=true;
                                gwdc006Table.config.cols[0][5].hide=true;
                                gwdc006Table.config.cols[0][6].hide=true;
                                gwdc006Table.config.cols[0][7].hide=true;
                                gwdc006Table.config.cols[0][8].hide=true;
                            }
                            if(vueData.form.GWDE002 == '外部返利类型'){
                                gwdc006Table.config.cols[0][9].hide=false;
                            }else {
                                gwdc006Table.config.cols[0][9].hide=true;
                            }
                            self.tableReload();
                        },
                        addData: function () {
                            listData = gwdc006Table.getDatas();
                            var count = 0;
                            if(listData.length > 0){
                                count = listData[listData.length-1].INDEX_;
                            }
                            listData.push({
                                INDEX_:count+1,
                                GWDE003: '',
                                GWDE004: ''
                            });
                            self.tableReload();
                        },
                        cancle: function () {
                            self._closeTpl();
                        },
                        delData: function () {
                            var list = gwdc006Table.getDatas();
                            var checkStatus = table.checkStatus('gwdc006_edit_table');
                            if (checkStatus.data.length > 0) {
                                checkStatus.data.forEach(function (v) {
                                    list.removeByMap({
                                        INDEX_: v.INDEX_
                                    });
                                });
                                listData = list;
                                self.tableReload();
                            }
                        },
                        saveData: function () {
                            this.$refs['form'].validate(function (valid) {
                                if (valid) {
                                    var listData_1 = gwdc006Table.getDatas();
                                    if(listData_1.length == 0){
                                        app.showError("表格数据不可为空！");
                                        return;
                                    }
                                    for (var i = 0; i < listData_1.length; i++) {
                                        var v = listData_1[i];
                                        if (v.GWDE003 == '' || v.GWDE003 == null || v.GWDE004 == '' || v.GWDE004 == null) {
                                            app.showWarning('选项值、选项名称不允许为空!');
                                            return;
                                        }
                                        if(vueData.form.GWDE002 == '外部返利类型'){
                                            if (v.GWDE017 == '' || v.GWDE017 == null) {
                                                app.showWarning('影响返利类型不允许为空!');
                                                return;
                                            }
                                        }
                                    }
                                    listData_1.addMap(vueData.form, false);
                                    if (vueData.isAdd) {
                                        self.addAllData(listData_1);
                                    } else {
                                        self.editAllData(listData_1);
                                    }
                                }
                            });
                        }
                    },
                    created: function () {
                        self._setEleIsReady('gwdc006_edit');
                    }
                });

                self.initTable();
            },
            _lazy: function () {
                this._super();
            }
        })
    ;

    return new Gwdc006Edit();

})
;
